<template>
  <!-- 首页-功能列表 -->
  <div>
    <van-skeleton title :row="8" :loading="loading">
      <div class="gm-card" v-for="item in centers" :key="item.Name">
        <div class="gm-card-header">
          {{ item.Name }}
        </div>
        <div class="gm-card-content">
          <van-grid icon-size="44px">
            <van-grid-item
              v-for="grid in item.Data"
              :key="grid.Name"
              :icon="grid.Icon"
              :text="grid.Name"
              :badge="handleNumber(grid.TaskQty)"
              @click="linkTo(grid)"
            />
          </van-grid>
        </div>
      </div>
    </van-skeleton>
    <van-skeleton title :row="8" :loading="loading" />
  </div>
</template>

<script>
import {
  Pad_GetApplicationCenter,
  Pad_EquipmentScanCode,
  Pad_EquipmentGetInfo,
} from "../../api/index";

export default {
  name: "Features",
  data() {
    return {
      loading: false,
      centers: [],
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      this.loading = true;
      Pad_GetApplicationCenter()
        .then((res) => {
          if (res.ReturnResult === "1") {
            this.centers = res.ReturnData.AppList;
          } else {
            this.$notify(res.Message);
          }
        })
        .catch((err) => {
          this.$notify(err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    linkTo(item) {
      if (item.Url) {
        const url = item.Url;
        if (url.startsWith("#/")) {
          const realUrl = url.split("#")[1];
          if (realUrl === "/start?qrcode=1") {
            // 测试
            // this.toStart("GM-A2-11-2")

            // 扫码报修
            const _this = this;
            window.wx.scanQRCode({
              desc: "scanQRCode desc",
              needResult: 1, // 默认为0，扫描结果由企业微信处理，1则直接返回扫描结果，
              scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是条形码（一维码），默认二者都有
              success: function (res) {
                _this.toStart(res.resultStr);
              },
              error: function (res) {
                if (res.errMsg.indexOf("function_not_exist") > 0) {
                  alert("版本过低请升级");
                }
              },
            });
          } else if (realUrl === "/sign-in") {
            // 测试
            // this.toSignIn("01.010-1180")

            // 扫码签到
            const _this = this;
            window.wx.scanQRCode({
              desc: "scanQRCode desc",
              needResult: 1,
              scanType: ["qrCode", "barCode"],
              success: function (res) {
                _this.toSignIn(res.resultStr);
              },
              error: function (res) {
                if (res.errMsg.indexOf("function_not_exist") > 0) {
                  alert("版本过低请升级");
                }
              },
            });
          } else {
            this.$router.push({
              path: realUrl,
            });
          }
        } else {
          window.location.href = item.Url;
        }
      }
    },
    toStart(code) {
      const params = {
        EquipmentNoDes: code,
      };
      Pad_EquipmentScanCode(params)
        .then((res) => {
          if (res.ReturnResult === "1") {
            this.$router.push({
              path: "/start",
              query: {
                qrcode: "1",
                equipmentInfo: JSON.stringify(res.ReturnData),
              },
            });
          } else {
            this.$dialog.alert({
              message: res.Message,
            });
          }
        })
        .catch((err) => {
          this.$dialog.alert({
            message: err,
          });
        });
    },
    toSignIn(code) {
      const params = {
        EquipmentNoDes: code,
      };
      Pad_EquipmentGetInfo(params)
        .then((res) => {
          if (res.ReturnResult === "1") {
            this.$router.push({
              path: "/sign-in",
              query: {
                equipmentInfo: JSON.stringify(res.ReturnData),
              },
            });
          } else {
            this.$dialog.alert({
              message: res.Message,
            });
          }
        })
        .catch((err) => {
          this.$dialog.alert({
            message: err,
          });
        });
    },
    handleNumber(num) {
      return num <= 0 ? "" : num >= 100 ? "99+" : num;
    },
  },
};
</script>

<style lang="scss" scoped>
.gm-card {
  background: #fff;
  margin: 10px 0;

  .gm-card-header {
    color: rgba(0, 0, 0, 0.85);
    font-size: 16px;
    padding: 0 15px;
    height: 42px;
    line-height: 42px;
  }
}
</style>